<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录注册</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link rel="stylesheet" type="text/css" href="/res/css/style.css" />
    <script src="/res/js/jquery-3.6.0.min.js"></script>
    <style type="text/css">
        #sex input{
            width: 10%;
            height: 30%;
            margin-left: 20px;
        }
        #area select{
            width: 30%;
            height: 50%;
        }
        #hobby input{
            width: 10%;
            height: 30%;
            margin-left: 20px;
        }
        #photo input{
            width: 50%;
            height: 50%;
        }
        #remark textarea{
            width: 80%;
            height: 80%;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="main">
        <div class="bg"></div>
        <form class="formone" action="/" method="get">
            <div class="title">登 录</div>
            <div class="username"><input type="text" name="username" id="name" v-model="username" value="" placeholder="请输入账号" /></div>
            <div class="password"><input type="password" name="username" v-model="password" id="pwd" value="" placeholder="请输入密码" /></div>
            <div class="verification"></div>
            <div class="loginbtn">
                <div class="btnbg" >登录</div><button id="btn_login"  type="button">登录</button>
            </div>
            <div class="registerbtn">
                <div class="btnbg" id="btn_register">注册</div><button  type="button">注册</button>
            </div>
        </form>
        <div class="registerpage">
            <form class="formtwo" action="/" method="" id="registerForm">
                <div class="registertitle">注册</div>
                <div class="savename"><input type="text" name="username" id="userName" value="" placeholder="请输入账号"/></div>
                <div class="savepassword"><input type="password" name="userpwd" id="userPwd" value="" placeholder="请输入密码"/></div>
                <div class="verification"></div>
                <div class="btn_list">
                    <div class="okbtn"><button type="button">确认</button></div>
                    <div class="resetbtn"><button type="button">撤销</button></div>
                </div>
            </form>

        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    let formone=document.querySelector('.formone');
    let registerpage=document.querySelector('.registerpage');
    let registerbtn=document.querySelector('.registerbtn');
    let loginbtn=document.querySelector('.loginbtn');
    let resetbtn=document.querySelector('.resetbtn');
    let okbtn=document.querySelector('.okbtn');
    registerbtn.addEventListener("click",()=>{
        formone.style.display='none';
        registerpage.style.display="flex";
    })
    loginbtn.addEventListener("click",()=>{
        console.info("触发");
        let data = {"username":$("#name").val(),"userpwd":$("#pwd").val()};
        let url = "/sUser/findLogin";
        $.post(url,data,function (res) {
            console.info(res);
            if (res.resp==1){
                console.info(res.user);
                //sessionStorage不能传对象，需要将其转换成字符串
                sessionStorage.setItem("user",JSON.stringify(res.data));
                location.href="/pages/index.html";
            }else {
                $(".verification").text("登录失败");
            }
        },"json")
    })

    $("#userName").on("blur",function (){
        let url = "/sUser/checkName";
        let data = {"userName":$(this).val()}
        $.post(url,data,function (res) {
            console.info(res);
            if (res.resp==1){
                $("#userName").val("");
                $(".verification").text(res.msg);
            }else {
                $(".verification").text("");
            }
        },"json")
    })

    okbtn.addEventListener("click",()=>{
        console.info("触发");
        let data =$("#registerForm").serialize();
        /*new FormData(document.getElementById("registerForm")) */
        console.info(data);
        let url = "/sUser/userAdd";
        $.ajax({
            async:true, //true异步.  false 同步
            type:"POST", //请求方式
            url:url,    //请求的路径
            data:data,  //请求的参数
            dataType:"json", //返回类型
            success:function(res){ //成功回调函数
                console.info(res);
                if (res.resp==1){
                    formone.style.display='flex';
                    registerpage.style.display="none";
                }else {
                    $(".verification").text(res.msg);
                }
            },error:function(){ //失败的回调函数

            }
        });

    })


    resetbtn.addEventListener("click",()=>{
        formone.style.display='flex';
        registerpage.style.display="none";
    })
</script>
</html>